package cn.labbook.bluetooth.controller.v2;

import cn.labbook.bluetooth.common.MyPage;
import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.controller.AbstractController;
import cn.labbook.bluetooth.entity.ApplyRecord;
import cn.labbook.bluetooth.pojo.dto.ApplyRecordDTO;
import cn.labbook.bluetooth.service.IApplyRecordService;
import cn.labbook.bluetooth.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zhangyang
 * @Date: 2023/4/14 14:13
 * @Desc: v2版本 申请记录 controller
 */
@Controller
@RequestMapping("/v2/applyRecord")
public class V2ApplyRecordController extends AbstractController {

    @Autowired
    private IApplyRecordService applyRecordService;


    @RequestMapping("/handle")
    @ResponseBody
    public ResultDto handle(
            @RequestParam Integer applyRecordId
    ) {
        applyRecordService.handle(applyRecordId);
        return success();
    }

    @RequestMapping("/back")
    @ResponseBody
    public ResultDto back(
            @RequestParam Integer applyRecordId
    ) {
        ApplyRecord applyRecord = applyRecordService.getById(applyRecordId);
        if (!Objects.equals(applyRecord.getAmount(), applyRecord.getPayTrueAmount())) {
            return fail("已同步保电，不可撤回");
        }
        applyRecordService.back(applyRecordId);
        return success();
    }

    @RequestMapping("/listWithTenant")
    @ResponseBody
    public ResultDto listWithTenant(String choiceDate, String openId, MyPage<ApplyRecord> page) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(choiceDate)) {
            map.put("time", choiceDate);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            map.put("time", sdf.format(new Date()));
        }

        // 到账金额
        Double totalAmount = 0.0d;
        // 申请金额
        Double applyAmount = 0.0d;
        List<ApplyRecord> sumPay = applyRecordService.getSumPay(choiceDate, null, openId);
        for (ApplyRecord applyRecord : sumPay) {
            if (Objects.isNull(applyRecord)) {
                continue;
            }

            if (Objects.equals(ApplyRecord.Status.IN.getKey(), applyRecord.getStatus())) {
                applyAmount += applyRecord.getAmount();
            } else if (Objects.equals(ApplyRecord.Status.COMPLETE.getKey(), applyRecord.getStatus())) {
                totalAmount += applyRecord.getAmount();
            }
        }
        BigDecimal bigDecimal = new BigDecimal(totalAmount);
        Double s = Double.parseDouble(bigDecimal.setScale(2, RoundingMode.HALF_UP).toString());
        BigDecimal bigDecimal1 = new BigDecimal(applyAmount);
        Double s1 = Double.parseDouble(bigDecimal1.setScale(2, RoundingMode.HALF_UP).toString());
        map.put("totalAmount", s);
        map.put("applyAmount", s1);

        List<ApplyRecordDTO> list = applyRecordService.listWithTenant(choiceDate, openId, page);
        map.put("list", list);
        return success(map);
    }

}
